package com.derek.omshopping.ui.home.adapter;

import android.content.Context;
import android.content.Intent;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.derek.omshopping.R;
import com.derek.omshopping.bean.home.HomeIndexListAllBean;
import com.derek.omshopping.ui.home.view.DetailsPageActivty;
import com.derek.omshopping.ui.home.view.OnItemClickListener;

import java.util.List;

/**
 * Created by duyb on 2016-10-05.
 * Desc:信息列表展示adapter
 */

public class HomeIndexAdapter extends RecyclerView.Adapter<HomeIndexAdapter.MyButtonViewHolder> {
    private List<HomeIndexListAllBean> list;
    private Context context;
    private OnItemClickListener listener;//点击事件监听 后面holder.itemVIew单击事件用到

    //listner 的set方法
    public void setListener(OnItemClickListener listener) {
        this.listener = listener;
    }

    public HomeIndexAdapter(Context context, List<HomeIndexListAllBean> list) {
        this.context = context;
        this.list = list;
        Log.e("index-adapter->",list.size()+"");
    }

    @Override
    public MyButtonViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        MyButtonViewHolder holder = new MyButtonViewHolder(LayoutInflater
                .from(context).inflate(R.layout.item_type_home_list, parent, false));
        return holder;
    }

    @Override
    public void onBindViewHolder(final MyButtonViewHolder holder, final int position) {
        holder.altDesc.setText(list.get(position).getBrand_name());
        holder.contsDesc.setText(list.get(position).getMobile_tag_txt());
        Glide.with(context).load(list.get(position).getItem_image()).centerCrop().into(holder.contsNm);
        holder.contsNm.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent=new Intent(context, DetailsPageActivty.class);
                String imgId=list.get(position).getConnect_tgt_addr();
                intent.putExtra("imgId",imgId);
                context.startActivity(intent);
            }

        });
       /* if (list != null) {
            //view点击事件为了动态得到itemview以及其位置的position
            holder.itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    int pos = holder.getLayoutPosition();
                    //接口回调实现监听
                    listener.onItemClick(holder.itemView, pos);
                }
            });
        }*/
    }

    @Override
    public int getItemCount() {
        return list.size();
    }

    class MyButtonViewHolder extends RecyclerView.ViewHolder {
        TextView contsDesc,altDesc;
        ImageView contsNm;

        public MyButtonViewHolder(View itemView) {
            super(itemView);
            contsDesc = (TextView) itemView.findViewById(R.id.home_list_conts_desc);
            altDesc = (TextView) itemView.findViewById(R.id.home_list_alt_desc);
            contsNm = (ImageView) itemView.findViewById(R.id.home_list_conts_nm);
        }
    }
}
